<?php

namespace app\models\user;

use Yii;

/**
 * This is the model class for table "user_consume".
 *
 * @property int $id
 * @property int $user_id
 * @property float $balance 使用余额
 * @property float $score 使用积分
 * @property float $ticket 使用购物券
 * @property float $platform_ticket 使用平台券
 * @property int $order_id 订单ID
 * @property int $cash_id 提现ID
 * @property int $merchant_order_id 商家订单ID
 * @property int $group_order_id 拼团ID
 * @property int $status 1 使用 0 退还
 * @property string $create_time
 * @property string $update_time
 * @property string $text 消费说明
 */
class UserConsume extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'user_consume';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['user_id', 'order_id', 'merchant_order_id', 'group_order_id', 'status'], 'integer'],
            [['balance', 'score', 'ticket', 'platform_ticket'], 'number'],
            // [['score', 'ticket'], 'required'],
            [['create_time', 'update_time'], 'safe'],

            [['text'], 'safe'],

            [['cash_id'], 'safe'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'user_id' => 'User ID',
            'balance' => 'Balance',
            'score' => 'Score',
            'ticket' => 'Ticket',
            'platform_ticket' => 'Platform Ticket',
            'order_id' => 'Order ID',
            'merchant_order_id' => 'Merchant Order ID',
            'group_order_id' => 'Group Order ID',
            'status' => 'Status',
            'create_time' => 'Create Time',
            'update_time' => 'Update Time',
        ];
    }

    public function beforeSave($insert)
    {
        if (parent::beforeSave($insert)) {
            $time = date('Y-m-d H:i:s');
            $this->update_time = $time;
            if ($insert) {
                $this->create_time = $time;
            }
            return true;
        }
        return false;
    }
}
